<?php

namespace app\api\model;

class UserExtra extends Common
{
    static public function change($user_id, $key, $value, $change=0, $toward=0)
    {
        $userExtra = self::get(['user_id' => $user_id]);

        if ($userExtra) {
            $where = ['user_id' => $user_id];

            if ($change) {
                if ($toward == 1) {
                    $where[$key] = $userExtra[$key] + $value;
                } else if ($toward == -1) {
                    $where[$key] = $userExtra[$key] - $value;
                }
            } else {
                $where[$key] = $value;
            }

            self::update($where);
        } else {
            $where = ['user_id' => $user_id];
            if ($change) {
                if ($toward == 1) {
                    $where[$key] = $value;
                }
            }

            self::create($where);
        }
    }
}
